package com.smmisia.article.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.smmisia.article.adapter.StorageAdapter;
import com.smmisia.common.Result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 文件上传
 * @author smmisia
 * @date 2023/7/27
 */
@Service
@Slf4j
public class FileService {

    private static final Set<String> ALLOWED_EXTENSIONS = new HashSet<>();

    static {
        ALLOWED_EXTENSIONS.add("jpg");
        ALLOWED_EXTENSIONS.add("jpeg");
        ALLOWED_EXTENSIONS.add("png");
        ALLOWED_EXTENSIONS.add("gif");
    }

    private final StorageAdapter storageAdapter;

    public FileService(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }

    /**
     * 列出所有桶
     */
    public List<String> getAllBucket() {
        return storageAdapter.getAllBucket();
    }

    /**
     * 获取文件路径
     */
    public String getUrl(String bucketName, String objectName) {
        return storageAdapter.getUrl(bucketName, objectName);
    }

    /**
     * 上传头像
     */
    public Result uploadFile(MultipartFile uploadFile) {
        String loginId = (String) StpUtil.getLoginId();
        String bucket = "avatar";
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String fileName = "avatar_" + loginId + "." + extension;
        String objectName = "images";
        if (!ALLOWED_EXTENSIONS.contains(extension)) {
            return Result.fail("文件非法");
        }
        storageAdapter.uploadFile(uploadFile, bucket, objectName);
        objectName = objectName + "/" + fileName;
        String url = storageAdapter.getUrl(bucket, objectName);
        HashMap<String, String> map = new HashMap<>();
        map.put("url", url);
        return Result.ok(map);
    }

    /**
     * 上传图片
     */
    public HashMap<String, String> upload(MultipartFile uploadFile) throws Exception {
        String bucket = "partner";
        String loginId = (String) StpUtil.getLoginId();
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String uuid = UUID.randomUUID().toString();
        String fileName = loginId + "_" + uuid + "." + extension;
        String objectName = loginId;
        if (!ALLOWED_EXTENSIONS.contains(extension)) {
            HashMap<String, String> error = new HashMap<>();
            error.put("error", "文件非法");
            return error;
        }
        storageAdapter.upload(uploadFile, bucket, objectName,uuid);
        objectName = objectName + "/" + fileName;
        String url = storageAdapter.getUrl(bucket, objectName);
        HashMap<String, String> map = new HashMap<>();
        map.put("url", url);
        return map;
    }

    public HashMap<String, String> qrcode(MultipartFile uploadFile) throws Exception {
        String bucket = "qrcode";
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String uuid = UUID.randomUUID().toString();
        String fileName = "QrCode_" + uuid + "." + extension;
        String objectName = "images";
        if (!ALLOWED_EXTENSIONS.contains(extension)) {
            HashMap<String, String> error = new HashMap<>();
            error.put("error", "文件非法");
            return error;
        }
        storageAdapter.uploadQrCode(uploadFile, bucket, objectName, uuid);
        objectName = objectName + "/" + fileName;
        String url = storageAdapter.getUrl(bucket, objectName);
        HashMap<String, String> map = new HashMap<>();
        map.put("url", url);
        return map;
    }

    public Result uploadImage(MultipartFile uploadFile) throws Exception {
        String bucket = "partner";
        String loginId = (String) StpUtil.getLoginId();
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String uuid = UUID.randomUUID().toString();
        String fileName = loginId + "_" + uuid + "." + extension;
        String objectName = loginId;
        if (!ALLOWED_EXTENSIONS.contains(extension)) {
            return Result.fail("文件非法");
        }
        storageAdapter.upload(uploadFile, bucket, objectName,uuid);
        objectName = objectName + "/" + fileName;
        String url = storageAdapter.getUrl(bucket, objectName);
        HashMap<String, String> map = new HashMap<>();
        map.put("url", url);
        return Result.ok(map);
    }

    public Result batchUpload(MultipartFile[] uploadFile) throws Exception {
        ArrayList<String> urlList = new ArrayList<>();
        String loginId = (String) StpUtil.getLoginId();
        for (MultipartFile file : uploadFile) {
            String bucket = "partner";
            String fileName = file.getOriginalFilename();
            String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
            String uuid = UUID.randomUUID().toString();
            fileName = loginId + "_" + uuid + "." + extension;
            String objectName = loginId;
            if (!ALLOWED_EXTENSIONS.contains(extension)) {
                return Result.fail("文件非法");
            }
            storageAdapter.upload(file, bucket, objectName,uuid);
            objectName = objectName + "/" + fileName;
            String url = storageAdapter.getUrl(bucket, objectName);
            urlList.add(url);
        }
        HashMap<String, List<String>> map = new HashMap<>();
        map.put("urlList", urlList);
        return Result.ok(map);
    }

    public Result uploadArticle(MultipartFile uploadFile) {
        String bucket = "article";
        String loginId = (String) StpUtil.getLoginId();
        String file = uploadFile.getOriginalFilename();
        String extension = file.substring(file.lastIndexOf(".") + 1);
        String uuid = UUID.randomUUID().toString();
        String fileName = loginId + "_" + uuid + "." + extension;
        String objectName = loginId;
        if (!ALLOWED_EXTENSIONS.contains(extension)) {
            throw new RuntimeException("文件非法");
        }
        try {
            storageAdapter.upload(uploadFile, bucket, objectName,uuid);
        } catch (RuntimeException e) {
            log.info("上传失败"+e.getMessage());
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        objectName = objectName + "/" + fileName;
        String url = storageAdapter.getUrl(bucket, objectName);
        return Result.ok(url);
    }

    public List<String> uploadBatchArticle(MultipartFile[] uploadFile) throws Exception {
        ArrayList<String> urlList = new ArrayList<>();
        String loginId = (String) StpUtil.getLoginId();
        for (MultipartFile file : uploadFile) {
            String bucket = "article";
            String fileName = file.getOriginalFilename();
            String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
            String uuid = UUID.randomUUID().toString();
            fileName = loginId + "_" + uuid + "." + extension;
            String objectName = loginId;
            if (!ALLOWED_EXTENSIONS.contains(extension)) {
                throw new RuntimeException("文件非法");
            }
            storageAdapter.upload(file, bucket, objectName,uuid);
            objectName = objectName + "/" + fileName;
            String url = storageAdapter.getUrl(bucket, objectName);
            urlList.add(url);
        }
        return urlList;
    }
}

